package com.edu.service;

import com.edu.bean.Post;
import com.edu.bean.PostReply;
import com.edu.dao.PostDao;
import com.edu.dao.PostReplyDao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AdminPostService {
    private PostDao postDao = new PostDao();
    private PostReplyDao postReplyDao = new PostReplyDao();

    /**
     * 分页获取帖子列表
     */
    public Map<String, Object> adminListPosts(int page, int limit, String title, Integer type) {
        Map<String, Object> result = new HashMap<>();
        int start = (page - 1) * limit;6
        List<Post> list = postDao.findByPage(start, limit, title, type);
        int total = postDao.findTotalCount(title, type);
        result.put("list", list);
        result.put("total", total);
        return result;
    }

    /**
     * 获取帖子详情
     */
    public Post adminGetPost(Integer postId) {
        Post post = postDao.findById(postId);
        if (post != null) {
            // 增加浏览次数
            postDao.incrementViewCount(postId);
        }
        return post;
    }

    /**
     * 删除帖子
     */
    public boolean adminDeletePost(Integer postId) {
        // 先删除帖子的所有回复
        postReplyDao.deleteByPostId(postId);
        // 再删除帖子
        return postDao.delete(postId);
    }

    /**
     * 获取帖子回复列表
     */
    public List<PostReply> adminListReplies(Integer postId) {
        return postReplyDao.findByPostId(postId);
    }

    /**
     * 添加回复
     */
    public boolean adminAddReply(PostReply reply) {
        return postReplyDao.add(reply);
    }
} 